import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn, STComponent, STData, STRequestOptions } from '@delon/abc/st';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { ChannelSalesService } from '../../services/level-config.service';
import { ParterLevelConfigEditComponent } from '../edit/edit.component';

@Component({
  selector: 'app-parter-LevelConfig-list',
  templateUrl: './list.component.html'
})
export class ParterLevelConfigListComponent implements OnInit {
  schema: SFSchema = {};
  columns!: STColumn[];
  ui!: SFUISchema;
  @ViewChild('st', { static: false })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  spuStatus = '1';

  data = [{ name1: 1111 }];
  constructor(
    public service: ChannelSalesService,
    private modalService: NzModalService
  ) {}

  /**
   * 查询参数
   */
  get reqParams() {
    return { ...this.sf?.value };
  }
  ngOnInit() {
    this.initSF();
    this.initST();
  }

  initSF() {
    this.schema = {
      properties: {
        gradeName: {
          type: 'string',
          title: '等级姓名'
        },
        stateLocked: {
          type: 'string',
          title: '状态',
          enum: [
            { label: '全部', value: '' },
            { label: '启用', value: '0' },
            { label: '禁用', value: '1' }
          ],
          ui: {
            widget: 'select'
          }
        }
      }
    };
    this.ui = {
      '*': {
        width: 300,
        grid: { span: 12, gutter: 4 }
      }
    };
  }

  initST() {
    this.columns = [
      {
        title: '等级姓名',
        index: 'gradeName',
        width: 200
      },
      {
        title: '备注',
        index: 'remark',
        width: 200
      },
      {
        title: '创建时间',
        index: 'createTime',
        width: 200
      },
     
      {
        title: '启用时间',
        index: 'enableTime',
        width: 200
      },
      {
        title: '排序',
        index: 'sortId',
        width: 200
      },
      {
        title: '状态',
        index: 'stateLocked',
        width: 200,
        format: (item: any) => {
          return item.stateLocked ? '禁用' : '启用';
        }
      },
      {
        title: '操作',
        className: 'text-center',
        width: 120,
        fixed: 'right',
        buttons: [
          {
            text: '编辑',
            acl: { ability: ['LevelConfig-edit'] },
            click: (_record, _modal, _instance) => this.edit(_record)
          },
          {
            text: '禁用',
            acl: { ability: ['LevelConfig-forbidden'] },
            click: (_record, _modal, _instance) => this.stop(_record),
            iif: item => !item.stateLocked
          },
          {
            text: '启用',
            acl: { ability: ['LevelConfig-startUseing'] },
            click: (_record, _modal, _instance) => this.restart(_record),
            iif: item => item.stateLocked
          }
        ]
      }
    ];
  }

  add() {
    const modalRef = this.modalService.create({
      nzWidth: 500,
      nzTitle: '新增',
      nzContent: ParterLevelConfigEditComponent,
      nzComponentParams: { type: this.spuStatus }
    });
    modalRef.afterClose.subscribe(res => {
      if (res) {
        this.st.reload();
      }
    });
  }

  // 编辑
  edit(record: STData) {
    const modalRef = this.modalService.create({
      nzWidth: 500,
      nzTitle: '编辑',
      nzContent: ParterLevelConfigEditComponent,
      nzComponentParams: { i: record, type: this.spuStatus }
    });
    modalRef.afterClose.subscribe(res => {
      if (res) {
        this.st.reload();
      }
    });
  }

  // 编辑
  view(record: STData) {
    const modalRef = this.modalService.create({
      nzTitle: '查看',
      nzContent: ParterLevelConfigEditComponent,
      nzComponentParams: { i: record }
    });
  }

  restart(item: any) {
    this.modalService.confirm({
      nzTitle: '<i>启用确认</i>',
      nzContent: `<b>确定启用该等级吗？</br>`,
      nzOnOk: () =>
        this.service.request(this.service.$api_updatePartnerGradeConfig, { id: item.id }).subscribe(res => {
          if (res) {
            this.service.msgSrv.success('启用成功！');
            this.st.reload();
          }
        })
    });
  }
  stop(item: any) {
    this.modalService.confirm({
      nzTitle: '<i>禁用确认</i>',
      nzContent: `<b>确定禁用该等级吗？</br>`,
      nzOnOk: () =>
        this.service.request(this.service.$api_updatePartnerGradeConfig, { id: item.id }).subscribe(res => {
          if (res) {
            this.service.msgSrv.success('禁用成功！');
            this.st.reload();
          }
        })
    });
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
  }
}
